var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/Attachment/views/formAttachment/components/ImageUpload/index.tsx
var ImageUpload_exports = {};
__export(ImageUpload_exports, {
  ImageUpload: () => ImageUpload
});
module.exports = __toCommonJS(ImageUpload_exports);
var import_antd = require("antd");
var import_react = require("react");
var import_util = require("../../../../../../util");
var import_AttachmentContext = require("../../../../AttachmentContext");
var import_components = require("../../../../components");
var getBase64 = (prefix, str) => {
  const content = Buffer.from(str, "utf8").toString("base64");
  return `${prefix};base64,${content}`;
};
var isConvertBase64 = (name) => {
  if (!name) {
    return false;
  }
  if (name.endsWith(".svg") || name.endsWith(".svgz")) {
    return true;
  }
  return false;
};
function useImageUpload(attachmentFileList = []) {
  const [fileList, setFileList] = (0, import_react.useState)([]);
  const [previewImage, setPreviewImage] = (0, import_react.useState)("");
  const [previewOpen, setPreviewOpen] = (0, import_react.useState)(false);
  const transformFile = (0, import_util.useRefCallback)((data) => {
    const url = (0, import_util.getIUPFilePreviewUrl)(data.asrFid).url;
    const extra = isConvertBase64(data.fileName || data.asrName) ? {
      originFileObj: new File(data.asrFid ? [data.asrFid] : [], url)
    } : { url };
    return {
      uid: data.asrFid || data.fileName,
      name: data.fileName,
      ...data,
      ...extra
    };
  });
  (0, import_react.useEffect)(() => {
    if (Array.isArray(attachmentFileList)) {
      const list = [...attachmentFileList].reverse().filter((v) => v.status !== "fail" && v.status !== "error");
      setFileList((pre) => {
        return list.map((item, index) => {
          if (pre[index] && pre[index].uid === item.asrFid) {
            return pre[index];
          }
          return transformFile(item);
        });
      });
    }
  }, [attachmentFileList]);
  const previewOptions = (0, import_react.useMemo)(() => {
    return {
      handlePreview: (file) => {
        if (!file.url && !file.preview && !file.thumbUrl) {
          console.log("preview file required url", file);
        } else {
          setPreviewImage(file.thumbUrl || file.preview || file.url || "");
          setPreviewOpen(true);
        }
      },
      onVisibleChange: (value, prevValue) => setPreviewOpen(value),
      afterOpenChange: (open) => !open && setPreviewImage("")
    };
  }, []);
  const previewFile = async (file) => {
    if (file instanceof File && file.size > 0) {
      const res = await import_util.iup.request.get({ url: file.name });
      return getBase64("data:image/svg+xml", res);
    }
    return Promise.resolve("");
  };
  const preview = {
    open: previewOpen,
    image: previewImage,
    ...previewOptions
  };
  return [fileList, { preview, previewFile }];
}
var defaultAccept = ".png,.jpg,.jpeg,.svg";
function ImageUpload(props) {
  const {
    attachmentFileList,
    uploadRef,
    accept = defaultAccept,
    maxCount = import_AttachmentContext.DEFAULT_ATTACHMENT_UPLOAD_MAX_COUNT,
    onRemove,
    onUploadStateChange,
    onBeforeUpload,
    onUploaded,
    disabled
  } = props;
  const [files, { preview, previewFile }] = useImageUpload(attachmentFileList);
  return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(
    import_components.Upload,
    {
      disabled,
      onUploadStateChange,
      onUploaded,
      onBeforeUpload,
      accept,
      ref: uploadRef,
      containerStyles: { display: "inherit" },
      onPreview: preview.handlePreview,
      showUploadList: true,
      listType: "picture-card",
      fileList: files,
      onRemove,
      previewFile
    },
    files.length >= maxCount ? null : /* @__PURE__ */ React.createElement("button", { style: { border: 0, background: "none" }, type: "button" }, /* @__PURE__ */ React.createElement(import_util.PlusOutlined, null), /* @__PURE__ */ React.createElement("div", { style: { marginTop: 8 } }, "上传图片"))
  ), preview.image && /* @__PURE__ */ React.createElement(
    import_antd.Image,
    {
      wrapperStyle: { display: "none" },
      preview: {
        visible: preview.open,
        onVisibleChange: preview.onVisibleChange,
        afterOpenChange: preview.afterOpenChange
      },
      src: preview.image
    }
  ));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ImageUpload
});
